package chances.epg.commons.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.support.RequestContextUtils;

public abstract class SpringWebUtils {
	public static <T> T findBean(HttpServletRequest request , Class<T> clazz) {
	
		WebApplicationContext appContext = RequestContextUtils.findWebApplicationContext(request,
				request.getServletContext());
		return appContext.getBean(clazz);
	}
	
	
	public static <T> T findBean(PageContext pageContext , Class<T> clazz) {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		return findBean(request,clazz);
	}
	
	
	public static Object findBean(HttpServletRequest request , String name) {
		
		WebApplicationContext appContext = RequestContextUtils.findWebApplicationContext(request,
				request.getServletContext());
		return appContext.getBean(name);
	}
	
	public static Object findBean(PageContext pageContext , String name) {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		return findBean(request,name);
	}
}
